<template>
  <div class="cont-box">
    <div class="time" v-if="quarterOption.length > 0">
      <div class="select-box">
        <!-- 季度下拉框 -->
        <van-dropdown-menu
          :overlay="false"
          :class="['select_quarter_item', fromPage === 'home' ? '' : 'select_full_item']">
          <van-dropdown-item
            @change="changeQuery"
            title-class="time_title"
            v-model="quarter"
            :options="quarterOption" />
        </van-dropdown-menu>
        <!-- 组织下拉框 -->
        <van-dropdown-menu v-if="fromPage === 'home' || isDry" :overlay="false" class="select_organ_item">
          <van-dropdown-item @change="changeOrgan" title-class="time_title" v-model="organInx" :options="organOption" />
        </van-dropdown-menu>
      </div>

      <div v-if="fromPage === 'home' || isDry" class="search-box">
        <van-field
          v-model.trim="paramData.keywords"
          center
          border
          size="small"
          class="search-input"
          placeholder="请输入关键字搜索"
          left-icon="search">
          <template #button>
            <van-button size="small" @click="getData">搜索</van-button>
          </template>
        </van-field>
        <!-- <div class="honor_num">“好”：{{ honorList.length }} 人</div> -->
        <!-- 等次下拉框 -->
        <div class="honor_num">
          <van-dropdown-menu v-if="fromPage === 'home' || isDry" :overlay="false" class="select_assess_item">
            <van-dropdown-item
              @change="changeAssessRank"
              title-class="time_title"
              v-model="assessRankInx"
              :options="dataReasonList" />
          </van-dropdown-menu>
        </div>
      </div>
    </div>
    <div class="self_roll" v-if="honorList.length > 0">
      <div class="con-item" v-for="(honor, index) in honorList" :key="index">
        <div class="item-left">
          <van-image
            class="user-icon"
            :src="honor.headUrl"
            round
            width="45px"
            height="45px"
            fit="cover"
            v-if="honor && honor.headUrl"></van-image>
          <van-image
            class="user-icon"
            :src="honor.gender === '女' ? circleUrlW : circleUrlM"
            round
            width="45px"
            height="45px"
            fit="cover"
            v-else></van-image>
        </div>
        <div class="item-right">
          <div class="right-top">
            <span class="user-name">{{ honor.userName }}</span>
            <span class="org-name">{{ honor.organName }}</span>
          </div>
          <div class="right-bottom">
            {{ getDesc(honor.busType, honor.grainDate) }}
            <span class="merits_honor_one_result" :class="getClass(honor.honorLevel)">{{ honor.honorLevel }}</span>
          </div>
        </div>
        <div class="item-img" v-if="getImg(honor.honorLevel)">
          <img :src="getImg(honor.honorLevel)" alt="" />
        </div>
      </div>
    </div>
    <van-empty v-else description="暂无数据,季度考核结束后查看" />
  </div>
</template>

<script>
import {
  selectListHonorRollDetailResponse,
  selectAllChoose,
  getHonorOrgList,
  orgGetHonorRollData,
  dryGetHonorRollData,
} from '@api/api-merits.js'
export default {
  name: 'honourRoll',
  data() {
    return {
      fromPage: '',
      quarter: 0,
      organInx: 0,
      quarterOption: [], // 季度下拉选择框
      organOption: [], // 组织下拉框
      dataReasonList: [
        { text: '全部等次', value: '' },
        { text: '好', value: '1' },
        { text: '较好', value: '2' },
        { text: '一般', value: '3' },
        { text: '较差', value: '4' },
        { text: '不定等次', value: '6' },
      ],
      departmentList: [], // 公示榜-组织帮-优秀机关科室
      honorList: [], // 公示榜-个人榜
      windowList: [], // 公示榜-组织榜-优秀大厅窗口

      paramData: {
        year: '2021',
        grainDate: 0,
        keywords: '',
        orgId: '',
        assessRank: '', //等次
      },
      isDry: false, // 判断是不是县干部
      assessRankInx: '',
      circleUrlM: require('@images/user-icon.png'),
      circleUrlW: require('@images/user-icon-G.png'),
      levelImgList: [
        require('@images/merits/mertis-hao.png'),
        require('@images/merits/mertis-jhao.png'),
        require('@images/merits/mertis-yban.png'),
        require('@images/merits/mertis-cha.png'),
      ],
      levelList: ['好', '较好', '一般', '较差'],
    }
  },
  created() {
    let { from = '' } = this.$route.query
    // this.fromPage = from
    this.fromPage = 'home'
    this.selectAllChoose()
  },
  methods: {
    // 普通员工获取某季度的公示榜
    generalGetData() {
      selectListHonorRollDetailResponse(this.paramData).then(res => {
        this.handleApi(res)
      })
    },
    // 组织部获取公示榜数据
    organGetData() {
      orgGetHonorRollData(this.paramData).then(res => {
        this.handleApi(res)
      })
    },
    // 县干获取公示榜
    dryGetData() {
      dryGetHonorRollData(this.paramData).then(res => {
        this.handleApi(res)
      })
    },
    /**
     * @function 处理接口数据
     * @param {Object} res 接口返回的数据
     */
    handleApi(res) {
      if (res.status === 200) {
        let { personList = [], windowList = [], departmentList = [] } = res.data || {}
        this.honorList = personList
        this.departmentList = departmentList
        this.windowList = windowList
      }
    },
    // 切换季度列表
    changeQuery() {
      let { year = '2021', grainDate = 1 } = this.quarterOption[this.quarter] || {}
      this.paramData.year = year
      this.paramData.grainDate = grainDate
      if (this.fromPage === 'home' || this.isDry) {
        this.getOrganChoose()
      }
      this.getData()
    },
    changeAssessRank() {
      this.paramData.assessRank = this.assessRankInx
      this.getData()
    },
    // 切换组织
    changeOrgan() {
      let { orgId = '' } = this.organOption[this.organInx] || {}
      this.paramData.orgId = orgId
      this.getData()
    },
    // 获取季度下拉框数据
    selectAllChoose() {
      selectAllChoose({}).then(res => {
        let { data = [] } = res
        if (data.length) {
          data.forEach((item, index) => {
            data[index] = Object.assign(item, {
              text: item.name,
              value: index,
            })
          })
          this.quarterOption = data
          this.paramData.year = data[0].year
          this.paramData.grainDate = data[0].grainDate
          if (this.fromPage === 'home' || this.isDry) {
            this.getOrganChoose()
          }
          this.getData()
        }
      })
    },
    // 县干部获取组织下拉框（包含非领导成员才展示）
    async getOrganChoose() {
      let param = {
        quarter: this.paramData.grainDate,
        type: this.fromPage === 'home' ? 1 : this.isDry ? 2 : 0, // 1-组织部，2-县干部
        year: this.paramData.year,
        assessRank: this.assessRank,
      }
      let { data = [] } = await getHonorOrgList(param)
      if (data.length) {
        data.unshift({
          orgId: '',
          orgName: '全部',
        })
        this.organInx = 0
        this.organOption = data.map((item, inx) => {
          return Object.assign(item, {
            text: item.orgName,
            value: inx,
          })
        })
        this.paramData.orgId = this.organOption[0].orgId
      }
    },
    // 根据不同权限展示不同的公示榜
    getData() {
      this.organGetData()
    },
    getDesc(busType, organId) {
      // let desc = busType === '1' ? '个人' : '组织'
      // const organ = {
      //   1: '第一季度',
      //   2: '第二季度',
      //   3: '第三季度',
      //   4: '第四季度',
      // }
      // desc += `${organ[organId]}考核结果：`
      // return desc
      return '考核结果：'
    },
    getImg(level) {
      const idx = this.levelList.findIndex(item => {
        return item === level
      })
      return this.levelImgList[idx]
    },
    getClass(level) {
      switch (level) {
        case '好':
          return 'level_1'
        case '较好':
          return 'level_2'
        case '一般':
          return 'level_3'
        case '较差':
          return 'level_4'
        default:
          break
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.cont-box {
  width: 100%;
  height: 100%;
  position: relative;
  padding-bottom: 30px;
  .time {
    line-height: 33px;
    font-size: 12px;
    background-color: #ffffff;
    padding: 10px 20px;
    margin-top: 10px;
    .search-box {
      margin-top: 10px;
    }
    .select-box,
    .search-box {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      .select_quarter_item,
      .search-input {
        width: 60%;
        /deep/.van-field__body {
          .van-field__control {
            padding-left: 10px;
          }
        }
        /deep/.van-button {
          border-top: none;
          border-bottom: none;
        }
      }
      .search-input {
        background: #f8f8f8;
        border-radius: 15px;
        padding-left: 10px;
        /deep/.van-button {
          background-color: #f8f8f8;
          color: #204dc2;
          padding-left: 0;
        }
      }
      .select_organ_item,
      .honor_num {
        width: calc(40% - 10px);
      }
      .select_assess_item {
        width: 100%;
      }
      .honor_num {
        // font-size: 14px;
        text-align: center;
      }
      .select_quarter_item.select_full_item {
        width: 100%;
      }
      .select_organ_item,
      .select_assess_item {
        /deep/.van-dropdown-menu__item {
          justify-content: center;
        }
      }
    }
    /deep/.van-dropdown-menu {
      position: relative;
      .van-dropdown-menu__bar {
        height: 33px;
        background-color: transparent;
        box-shadow: none;
        justify-content: flex-end;
        .time_title {
          font-size: 13px;
          color: #333333;
        }
      }
      .van-dropdown-item {
        position: absolute;
        height: 190px;
        top: 36px !important;
        width: 100%;
        z-index: 9999;
        .van-cell {
          padding: 5px;
        }
        .van-cell__title {
          font-size: 12px;
          text-align: center;
        }
        .van-cell__value {
          display: none;
        }
        .van-popup {
          position: absolute;
        }
      }
      .van-dropdown-menu__item {
        justify-content: flex-start;
        background-color: #ffffff;
        // flex: none;
        padding-right: 16px;
        padding-left: 16px;
      }
    }
  }
  .self_roll {
    height: 100%;
    position: relative;
    font-size: 13px;
    padding: 8px 16px 60px;
    background: #f5f5f5;
    .con-item {
      display: flex;
      align-items: center;
      // height: 72px;
      background-color: #ffffff;
      line-height: 24px;
      padding: 10px 15px;
      position: relative;
      margin-top: 8px;
      border-radius: 5px;
      .item-left {
        width: 45px;
        height: 45px;
      }
      .item-right {
        margin-left: 20px;
        font-size: 13px;
        color: #666666;
        flex: 1;
        .right-top {
          max-width: calc(100% - 35px);
          display: flex;
          flex-wrap: wrap;
          align-items: center;
        }
        .user-name {
          font-size: 14px;
          font-weight: bold;
          margin-right: 10px;
          color: #000000;
        }
        .right-bottom {
          font-size: 14px;
          color: #333333;
          .merits_honor_one_result {
            font-size: 16px;
            font-weight: bold;
            color: #999999;
          }
          .level_1 {
            color: #2dc005;
          }
          .level_2 {
            color: #29bfc5;
          }
          .level_3 {
            color: #c5a229;
          }
          .level_4 {
            color: #666666;
          }
        }
      }
      .item-img {
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        width: 30px;
        height: 40px;
        img {
          width: 30px;
          height: 40px;
        }
      }
      .user-icon {
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>
